const app = getApp();
const model = require('../../../utils/model');
// pages/my/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",
    user:{},
    list: [],
    searchList:[],
    scrollHeight: 0,
    lastId:0,
    canLoadMore:true,
    loadtips:'数据加载中...',
    showLoading:false
  },

  showInput: function () {
      this.setData({
          inputShowed: true
      });
  },
  hideInput: function () {
      this.setData({
          inputVal: "",
          inputShowed: false,
          searchList:[]
      });
  },
  clearInput: function () {
      this.setData({
          inputVal: "",
          searchList:[]
      });
  },
  inputTyping: function (e) {
      this.setData({
          inputVal: e.detail.value
      });

      var me = this;

      model.request({
        action:'searchRankingList',
        data: {
          search:e.detail.value,
          last_id:0
        },
        success: function (res) {
          if (res.data.list && res.data.list.length) {
            me.setData({
              searchList:res.data.list
            });
          }
          else {
            me.setData({
              searchList:[]
            });
          }
        }
      });
  },

  getUserInfo:function () {
    var me = this;
    model.request({
      action:'userinfo',
      success: function (res) {
        //console.log(res.data.flash_list);
        me.setData({
          user:res.data.detail
        });
      }
    });
  },

  getList: function () {
    if (!this.data.canLoadMore){
      return;
    }
    this.data.canLoadMore = false;
    var me = this;
    model.request({
      action:'rankingList',
      data:{
        last_id:me.data.lastId
      },
      success: function (res) {
        me.data.canLoadMore = true;
  
        if (res.data.list && res.data.list.length) {
          var newList = me.data.list.concat(res.data.list);
          me.setData({
            list:newList,
            lastId:res.data['last_id'],
            showLoading:false
          });
        }
        else {
          me.setData({
            notShowLoadingFlag:true,
            loadtips:'暂无更多数据'
          });
        }
      }
    });
  },
  
  scroll:function () {
    this.setData({
      showLoading:true
    });
  },

  lower: function () {
    this.getList();
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      scrollHeight: app.globalData.winHeight - 93
    });
    this.getUserInfo();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})